<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace backend\controllers;

use builder\helper\H5;
use common\helper\App;
use Yii;
use builder\base\BaseController;
use common\models\Admin;

/**
 *
 * @author cleverstone
 * @since ym2.0
 */
class HomeController extends BaseController
{
    public $actionVerbs = [
        'index' => ['GET'],
        'account' => ['GET', 'POST'],
    ];
    public $undetectedActions = ['index', 'account'];

    /**
     * 控制面板
     * @return string
     */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 账户设置
     * @return string
     * @throws \Throwable
     */
    public function actionAccount()
    {
        /** @var Admin $user */
        $user = $this->user->identity;
        if ($this->isPost) {
            $bodyParams = $this->filteredPost;
            $bodyParams['photo'] = App::getFileLinkFromParams($bodyParams, 'photo');
            $user->setScenario('account_setting');
            $user->load($bodyParams);
            if (!$user->validate()) {
                return $this->asFail($user->error);
            }

            if (!empty($bodyParams['password_hash'])) {
                if (empty($bodyParams['password_hash_repeat'])) {
                    return $this->asFail('请重复输入密码');
                }
                if (strcmp($bodyParams['password_hash'], $bodyParams['password_hash_repeat'])) {
                    return $this->asFail('两次输入的密码不一样');
                }

                $user->setPassword($bodyParams['password_hash']);
            }

            $user->save(false);
            return $this->asOk("账号【{$user->username}】编辑成功");

        } else {
            $relativeCreated = Yii::$app->formatter->asRelativeTime(strtotime($user->created_at));
            $relativeUpdated = Yii::$app->formatter->asRelativeTime(strtotime($user->updated_at));

            $viewBuilder = $this->viewBuilder;
            $form = $viewBuilder->form;
            $form->setId('home_account')
                ->setTitle('账户设置')
                ->setRequiredStyle()
                ->setGroup('账户设置', 'account_setting')
                ->setGroupCol(6)
                ->setFooterLayout(['class' => 'col-md-6'])
                ->setFormControl([
                    'photo' => $this->formControl->file()->label('头像')->scenarioImage()->max(1)->defaultValue($user->photo),
                    'password_hash' => $this->formControl->password()->label('密码'),
                    'password_hash_repeat' => $this->formControl->password()->label('重复密码'),
                    'email' => $this->formControl->text()->label('邮箱')->defaultValue($user->email),
                    'mobile' => $this->formControl->text()->label('手机号码')->defaultValue($user->mobile),
                    'created_at' => $this->formControl->staticText()->label('注册时间')->defaultValue($user->created_at . H5::text(" ($relativeCreated) ", ['class' => 'text-primary'])),
                    'updated_at' => $this->formControl->staticText()->label('上次更新时间')->defaultValue($user->updated_at . H5::text(" ($relativeUpdated) ", ['class' => 'text-primary'])),
                ])
                ->setSubmitConfirm()
                ->setResetBtn()
                ->setSubmitBtn('保存设置');

            return $viewBuilder->render();
        }
    }
}